package com.flexcloudsdk.mail;
import java.io.IOException;

import java.util.logging.Logger;
import com.google.appengine.api.mail.MailService;
import com.google.appengine.api.mail.MailServiceFactory;
import com.google.appengine.api.mail.MailService.Message;

public class CloudMailService {
	private static final Logger log = Logger.getLogger(CloudMailService.class.getName());
	private MailService service = null;
	private String sourceClass;
	private String sourceMethod;

	
	public CloudMailService() {
		log.info("CloudMailService has been created by BlazeDS");
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		sourceClass = CloudMailService.class.getName();
		service = MailServiceFactory.getMailService();
	}
	
	public void send(Message message) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
	
		try {
			service.send(message);
		} catch (IOException exception) {
			log.severe("IOException when sending a message using CloudMailService");	
		}
	}
	
	public void sendToAdmins(Message message) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
	
		try {
			service.sendToAdmins(message);
		} catch (IOException exception) {
			log.severe("IOException when sendToAdmins a message using CloudMailService");	
		}
	}
	
}
